# -*- coding = utf-8 -*-
# @Time      : 2020/10/17 21:23
# @Author    : Reanon
# @File      : testM2.py
# @Email     : dafo360@gmail.com
# @Software  : PyCharm

import pandas as pd
import requests
from bs4 import BeautifulSoup

# 1.请求地址url


url = "http://spec.org/power_ssj2008/results/power_ssj2008.html"
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36"
}
html = requests.get(url, headers=headers)
if html.status_code == 200:
    soup = BeautifulSoup(html.content, 'lxml', from_encoding='utf-8')
    table = soup.find('table')
    tf = pd.read_html(table.prettify(), header=0)  # prettify():页面美化（整理成有格式的）

    for table in pd.read_html(url):
        table.to_csv('./results.csv', mode='a', encoding='utf-8', header=0, index=False)
    # print(tf[0])
    # tf[0].to_csv('results.csv', header=None, encoding='utf-8-sig', mode='a')  # mode = a 持续往进写值
else:
    print('输出失败')
